/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.data;

import aptech.semester.code.conn.MyLib;
import aptech.semester.code.entity.DepartmentEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author vi
 */
public class DepartmentList {
    private static final String QUERY_ALL_DEPARTMENT = "SELECT * FROM DEPARTMENT";
    
    public List<DepartmentEntity> getAllDepartment() {
        List<DepartmentEntity> listDepartment = new ArrayList<>();
        
        try {          
            Connection cn = MyLib.getCN();
            if(cn != null) {
                Statement st = cn.createStatement();
                 ResultSet rs = st.executeQuery(QUERY_ALL_DEPARTMENT);
                 
                while(rs.next()) {
                    DepartmentEntity dept = new DepartmentEntity();
                    dept.setDepartmentId(rs.getInt("ID"));
                    dept.setDepartmentName(rs.getString("DEPT_NAME"));
                    dept.setDescription(rs.getString("DESCRIPTION"));
                     
                    listDepartment.add(dept);
                }
                rs.close();
                cn.close();
            }           
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return listDepartment;
    }
    
     //-------------------------INSERT-------------------------
    
    public boolean insert(DepartmentEntity d){
       try{
           // 1.Tao 1 ket noi
           Connection cn = MyLib.getCN();
           // 2.Tao doi tuong chua cau lenh sql
           String sql = "Insert DEPARTMENT values (?,?)";
           PreparedStatement pst = cn.prepareStatement(sql);
           
           pst.setString(1, d.getDepartmentName());
           pst.setString(2, d.getDescription());
           // 3. Cho thi hanh cau lenh sql
           int n = pst.executeUpdate();
           pst.close();
           cn.close();
           return (n>0);
       }catch(Exception e){
           JOptionPane.showMessageDialog(null, "Department name existed.Please enter different name!");
          return false;
       }
        
    }
    
    //-------------------------UPDATE-------------------------
   
    public boolean update(DepartmentEntity d){
        try{
            // 1.Tao 1 ket noi
            Connection cn = MyLib.getCN();
            // 2.Tao doi tuong chua cau lenh update sql
            String sql = "Update DEPARTMENT set DEPT_NAME = ?, DESCRIPTION = ? where ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            
            pst.setString(1, d.getDepartmentName());
            pst.setString(2, d.getDescription());
            pst.setInt(3, d.getDepartmentId());
            
            // 3.Cho thi hanh cau lenh sql
            int r = pst.executeUpdate();
            pst.close();
            cn.close();
            return( r>0);
        }catch(Exception e){
            e.printStackTrace();
        }
        
        return false;
    }
    
     //-------------------------UPDATE-------------------------
    
    public boolean delete(int Id){
        try{
            // 1.Tao 1 ket noi
            Connection cn = MyLib.getCN();
            // 2.Tao doi tuong chua cau lenh delete sql
            String sql = "Delete from DEPARTMENT where ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            
            pst.setInt(1, Id);
            // 3. Cho thi hanh cau lenh sql
            int a = pst.executeUpdate();
            pst.close();
            cn.close();
            return(a>0);
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Can not delete beccase of having employee belong this department!");
        }
        return false;
    }
}
